package repository

import (
	"fmt"
	"gluenet/modules/manager/server/adapter/repository/mysql"
	"gluenet/modules/manager/server/domain/dependency"
	"gluenet/modules/manager/server/domain/entity"
	"gluenet/modules/manager/server/infra"
	"gluenet/modules/manager/server/infra/prepare"
)

func init() {
	prepare.RegisterRepo(func() {
		repo := NewIndexRepo()
		prepare.InjectDependency(func() dependency.IndexRepo {
			return repo
		})
	})
}

type Index struct {
}

func (i Index) Get(userId string) (*entity.Index, error) {
	var obj []entity.Index
	if err := mysql.DB.Where(map[string]interface{}{
		"user_id": userId,
	}).Find(&obj); err != nil {
		return nil, err
	}

	if len(obj) > 1 {
		return nil, fmt.Errorf("much primary keys in sql")
	}

	if len(obj) == 0 {
		return nil, infra.DataNotFind
	}

	return &obj[0], nil
}

func (i Index) FindBy(m map[string]string) ([]entity.Index, error) {
	//TODO implement me
	panic("implement me")
}

func (i Index) Del(user_id string) (*entity.Index, error) {
	//TODO implement me
	panic("implement me")
}

func NewIndexRepo() *Index {
	r := &Index{}
	return r
}
